<template>
  <div class="app-container">
     <el-card class="filter-container" shadow="never">
        <div>
          <i class="el-icon-search"></i>
          <span>{{$t('common.filterSearch')}}</span>
            <el-button
              style="float: right"
              @click="searchQAlist(list.questionId,list.questionTitle,list.type)"
              type="primary"
              size="small">
              {{ $t('common.search') }}
            </el-button>
        </div>
        <div style="margin-top: 15px">
          <el-form :inline="true" :model="list" size="small" label-width="140px">
            <el-form-item label="搜索问题ID：">
              <el-input style="width: 203px" v-model="list.questionId" placeholder="问题ID"></el-input>
            </el-form-item>
             <el-form-item label="搜索关键字：">
                <el-input style="width: 203px margin-left" v-model="list.questionTitle" placeholder="问题名称或者关键字"></el-input>
            </el-form-item>
            <el-form-item label="搜索问题类型：">
                <el-input style="width: 203px margin-left" v-model="list.type" placeholder="问题类型"></el-input>
            </el-form-item>
          </el-form>
        </div>
    </el-card>

    <div align="right">
      <el-button type="success" @click="addQA">新增问题</el-button>
    </div>

    <el-table
      :data="list"
      border
      fit
      highlight-current-row
    >

      <el-table-column label="问题ID" width="100">
        <template slot-scope="scope">
          {{ scope.row.questionId }}
        </template>
      </el-table-column>

      <el-table-column label="问题标题">
        <template slot-scope="scope">
          {{ scope.row.questionTitle }}
        </template>
      </el-table-column>

      <el-table-column label="回答">
        <template slot-scope="scope">
          {{ scope.row.answer }}
        </template>
      </el-table-column>

      <el-table-column label="提问时间">
        <template slot-scope="scope">
          {{ scope.row.createTime }}
        </template>
      </el-table-column>

      <el-table-column label="回复时间">
        <template slot-scope="scope">
          {{ scope.row.updateTime }}
        </template>
      </el-table-column>

      <el-table-column :label="$t('common.operate')" align="center" width="230" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            type="info"
            size="mini"
            icon="el-icon-view"
            @click="showDetail(scope.row.articleId)"
            >{{ $t('common.see') }}
          </el-button
          >
          <el-button type="primary" size="mini" @click="editQA(scope.row.questionId)">
            {{ $t('common.update') }}
          </el-button>
          <el-button size="mini" type="danger" @click="delQA(scope.row.questionId)">
            {{ $t('common.delete') }}
          </el-button>

        </template>
      </el-table-column>
    </el-table>
<!-- 分页----------------------------- -->
   <div class="block" style="float:right ;margin-top: 20px;">
    <!-- <span class="demonstration">显示总数</span> -->

    <el-pagination
      background
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page.sync="listMsg.page_num"
      :page-size="listMsg.page_size"
      layout="total, prev, pager, next"
      :total="listMsg.total">
    </el-pagination>
  </div>
<!-- ------------------------------------ -->
 <!-- ---------------------------------------查看对话框-->
    <el-dialog title="查看文章" :visible.sync="dialogFormVisible">
      <el-form style="width: 80%" :model="detail">
        <el-form-item label="文章名称" label-width="100px">
          <el-input autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="文章图片" label-width="100px">
          <el-upload
            class="avatar-uploader"
            action="https://jsonplaceholder.typicode.com/posts/"
            :show-file-list="false"
              :on-success="handleAvatarSuccess"
            :before-upload="beforeAvatarUpload"
          >
            <img v-if="imageUrl" :src="imageUrl" class="avatar" />
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
             <div slot="tip" class="el-upload__tip">只能上传jpg/png文件，且不超过500kb</div>
          </el-upload>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="dialogFormVisible = false"
          >确 定</el-button
        >
      </div>
    </el-dialog>
    <!-- --------------------------------------- -->
  </div>
</template>

<script>
import healqa from "@/api/healthyQA";
export default {

  data() {
    return {
     list: [], //列表条件封装对象
      page_num: 1,
      page_size: 5, //每5个数据一页
      total: 1, //初始化
      dialogFormVisible: false, //对话框（查看）
    }
  },
  created() {
    this.fetchData()
  },
  methods: {
    //查看文章
    showDetail() {
      this.dialogFormVisible = true;
    },
    addQA() {
      this.$router.push('/HealthyQA/addqa')
    },
     editQA(questionId){
      this.$router.push("/HealthyQA/editqa/"+questionId)
    },
    //页面列表页渲染，请求接口调用
    fetchData(page = 1) {
      this.page_num = page;
      var vm = this;
      healqa.getHealthyQA(this.page_num, this.page_size)
        //请求成功调用
        .then(function (res) {
          vm.list = res.data.questions;
          vm.total = res.data.total;
        });
    },


    //size-change和current-change事件来处理页码大小和当前页变动时候触发的事件。
     handleSizeChange(val) {


        //console.log(`每页 ${val} 条`);
        //this.list.page_num = 1;
        this.listMsg.page_size=val;
        this.fetchData();
      },


      handleCurrentChange(e) {


        //console.log(`当前页: ${val}`);
        this.listMsg.page_num = e
        this.fetchData()
      }

  }
}
</script>
